package com.cjc.hotel.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjc.hotel.server.entity.vo.AdminVo;
import com.cjc.hotel.server.handler.CommonException;
import com.cjc.hotel.server.result.Result;
import com.cjc.hotel.server.utils.ResultUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjc.hotel.server.entity.TAdmin;
import com.cjc.hotel.server.mapper.TAdminMapper;
import com.cjc.hotel.server.service.TAdminService;
/**
 * @author chenjuncai
 */
@Service
public class TAdminServiceImpl extends ServiceImpl<TAdminMapper, TAdmin> implements TAdminService{
    /**
     * 登录
     * @param adminVo 参数
     * @return 结果
     */
    @Override
    public Result<String> login(AdminVo adminVo, HttpServletRequest request) {
        QueryWrapper<TAdmin> tAdminQueryWrapper = new QueryWrapper<>();
        tAdminQueryWrapper.eq("username",adminVo.getUsername());
        tAdminQueryWrapper.eq("password",adminVo.getPassword());
        TAdmin tAdmin = this.baseMapper.selectOne(tAdminQueryWrapper);
        if(null == tAdmin){
            throw new CommonException("用户名或密码出错！");
        }
        request.getSession().setAttribute("admin", tAdmin);
        return ResultUtil.define(200,"登录成功！","");
    }
    /**
     * 注册
     * @param adminVo 参数
     * @return 结果
     */
    @Override
    public Result<String> register(AdminVo adminVo) {
        TAdmin tAdmin = new TAdmin();
        BeanUtils.copyProperties(adminVo,tAdmin);
        int insert = this.baseMapper.insert(tAdmin);
        return insert>0?ResultUtil.fail("注册成功！"):ResultUtil.define(200,"注册失败!","");
    }
}
